﻿Imports SIGI.Common.BE

Public Class UC_AppointmentPreviewPanel

#Region "<< -Attributes- >>"
    Private _AptList As List(Of AppointmentBE) = Nothing
    Private _Refresh As Boolean = False
#End Region

#Region "<< -Events- >>"
    Private Sub UC_AppointmentPreviewPanel_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If Me.DesignMode Then Return
        LoadAppointmentList()
        Timer.Interval = 60000 '1 min
        Timer.Start()
    End Sub

    Private Sub pnlApt_ControlRemoved(sender As System.Object, e As System.Windows.Forms.ControlEventArgs) Handles pnlApt.ControlRemoved
        If _Refresh Then LoadAppointmentList()
    End Sub

#End Region

#Region "<< -Methods- >>"
    Private Sub LoadAppointmentList()
        Dim wAptController As AppointmentController = Nothing
        Dim wApt As AppointmentBE = Nothing
        Dim wUc_Apt As UC_AppointmentPreview = Nothing

        wAptController = New AppointmentController
        _AptList = wAptController.SearchByDate(Date.Today, GlobalVariables.gLoguedUser.UserName)

        If _AptList.Count = 0 Then
            lblNoDates.Visible = True
            pnlApt.Visible = False
        Else
            lblNoDates.Visible = False
            pnlApt.Visible = True
            pnlApt.Controls.Clear()
            For Each wApt In _AptList
                wUc_Apt = New UC_AppointmentPreview(wApt)
                pnlApt.Controls.Add(wUc_Apt)
                wUc_Apt.Dock = DockStyle.Left
            Next
        End If
    End Sub

    Private Sub Timer_Tick(sender As System.Object, e As System.EventArgs) Handles Timer.Tick
        Dim wTmp As Date = Date.MinValue
        Dim wAnounceAt As String = String.Empty
        Dim wCurTime As String = String.Empty
        Dim wFrmApt As frmAppointmentViewer = Nothing
        Dim wApt As AppointmentBE = Nothing

        'Refresco la lista de citas por si hubo nuevas.
        _Refresh = False
        LoadAppointmentList()
        _Refresh = True
        If _AptList Is Nothing OrElse _AptList.Count = 0 Then Return

        'Verifico si hay alguna que anunciar.
        For wIdx As Integer = 0 To _AptList.Count - 1
            wApt = _AptList(wIdx)
            wCurTime = Date.Now.ToShortTimeString()
            wTmp = DateAdd(DateInterval.Minute, -wApt.Warning.Value, wApt.DateStart.Value)
            wAnounceAt = wTmp.ToShortTimeString()
            If wAnounceAt = wCurTime Or DateDiff(DateInterval.Minute, Date.Now, wApt.DateStart.Value) <= 0 Then
                wFrmApt = New frmAppointmentViewer(wApt, frmAppointmentViewer.Mode.Warning)
                wFrmApt.ShowDialog()
                If wFrmApt.IsDiscarded() Then
                    DirectCast(pnlApt.Controls(wIdx), UC_AppointmentPreview).Discard()
                End If
            End If
        Next
    End Sub

    Private Sub btnRefresh_Click(sender As System.Object, e As System.EventArgs) Handles btnRefresh.Click
        _Refresh = False
        LoadAppointmentList()
        _Refresh = True
    End Sub
#End Region

#Region "<< -Methods- >>"
    Public Sub StopTimer()
        Timer.Stop()
    End Sub
#End Region

End Class

